@extends('admin.layout.base')
@section('styles')
@endsection
@section('content')
    <div class="panel-content" id="app" v-cloak>
        <div class="panel-body">
            <div class="panel-header">
                <div class="panel-title"> 店铺管理 </div>
                <el-tabs v-model="tabValue" type="card" @tab-click="search()">
                    <el-tab-pane label="全 部" name="0"></el-tab-pane>
                    <el-tab-pane label="首页推荐" name="5"></el-tab-pane>
                    <el-tab-pane label="右侧推荐(前端显示3个)" name="6"></el-tab-pane>
                    <el-tab-pane label="装修中" name="7"></el-tab-pane>
                    <el-tab-pane label="正 常" name="1"></el-tab-pane>
                    <el-tab-pane label="待审核" name="2"></el-tab-pane>
                    <el-tab-pane label="未通过" name="3"></el-tab-pane>
                    <el-tab-pane label="下架" name="4"></el-tab-pane>
                </el-tabs>
            </div>
            <div class="panel-main">
                <div class="panel-btns">
                    <el-button type="success" icon="fa fa-refresh" @click="list()" size="small"> 刷新</el-button>
                    <el-button type="primary" icon="fa fa-plus" @click="exports()" size="small"> 导出</el-button>
                    {{--<el-button type="info" icon="fa fa-trash" @click="multiDelete()" size="small"> 删除</el-button>--}}
                </div>
                <div class="panel-search">
                    <div class="panel-search-item">
                        <el-input  size="medium" v-model="params.kwd" clearable placeholder="请输入店铺名" class="input-with-select">
                            <el-button type="primary" @click="search()" slot="append" icon="el-icon-search"></el-button>
                        </el-input>
                    </div>
                    <div class="panel-search-item">
                        <el-cascader size="medium" clearable placeholder="全部类目"
                                     separator=">"
                                     :show-all-levels="false"
                                     :options="categoys"
                                     @change="cascaderChange"
                                     v-model="cateCascader">
                        </el-cascader>
                    </div>
                    <div class="panel-search-item">
                        <el-date-picker
                                size="medium"
                                v-model="date"
                                type="daterange"
                                @change="pickerChange"
                                range-separator="至"
                                start-placeholder="开始日期"
                                end-placeholder="结束日期">
                        </el-date-picker>
                    </div>
                </div>
                <el-table v-loading="loading"
                          element-loading-text="努力加载中..."
                          element-loading-spinner="el-icon-loading"
                          ref="multipleHandle"
                          tooltip-effect="dark"
                          @selection-change="handleSelectionChange"
                          :data="listData.data" border>
                    <el-table-column align="center" type="selection" width="40"></el-table-column>
                    <el-table-column align="center" prop="logo" label="LOGO" width="125">
                        <template slot-scope="scope">
                            <el-popover placement="right" trigger="click">
                                <img :src="scope.row.logo" style="width: 550px;height: auto"/>
                                <el-button slot="reference"
                                           style="border: none;height: 50px;padding: 0;overflow: hidden">
                                    <img title="点击查看原图" alt="点击查看原图" :src="scope.row.logo"
                                         style="height: 50px;width: 100px"/>
                                </el-button>
                            </el-popover>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="store_name" label="店铺名" width="150" :show-overflow-tooltip="true"></el-table-column>
                    <el-table-column align="center" prop="call" label="致电量" width="90"></el-table-column>
                    <el-table-column align="center" prop="credit" label="好评率" width="90"></el-table-column>
                    <el-table-column align="center" prop="collections" label="收藏量" width="90"></el-table-column>
                    <el-table-column align="center" prop="sales" label="销量" width="85"></el-table-column>
                    <el-table-column align="center" prop="created_at" label="创建日期" width="165"></el-table-column>
                    <el-table-column align="center" prop="index_recomment" label="首页推荐" width="80">
                        <template slot-scope="scope">
                            <span class="el-tag" v-if="scope.row.index_recomment !==0">是</span>
                            <span class="el-tag el-tag--danger" v-else>否</span>
                        </template>
                    </el-table-column>
                    <el-table-column align="center" prop="status_text" label="状态" width="80">
                        <template slot-scope="scope"><span v-html="scope.row.status_text"></span></template>
                    </el-table-column>
                    <el-table-column label="操作" >
                        <template slot-scope="scope">
                            <el-button @click="show(scope.row.id)" icon="fa fa-adn" type="warning" size="mini" plain>预览</el-button>
                            <el-button v-if="scope.row.status != 2" @click="detail(scope.row.id)" icon="fa fa-bar-chart" type="success" size="mini" plain>店铺统计</el-button>
                            <el-button @click="edit(scope.row.id)" icon="fa fa-edit" type="primary" size="mini" plain>详情</el-button>
                            <el-button v-if="scope.row.status == 2" @click="active(scope.row.id)" icon="fa fa-unlock-alt" type="success" size="mini" plain>同意</el-button>
                            <el-button v-if="scope.row.status == 2" @click="lock(scope.row.id)" icon="fa fa-lock" type="danger" size="mini" plain>拒绝</el-button>
                            <el-button v-if="scope.row.status == 4" @click="active(scope.row.id)" icon="fa fa-unlock-alt" type="success" size="mini" plain>上架</el-button>
                            <el-button v-if="scope.row.status == 1" @click="close(scope.row.id)" icon="fa fa-lock" type="danger" size="mini" plain>下架</el-button>
                            {{--<el-button @click="destroy(scope.row.id)" icon="fa fa-trash" type="info" size="mini" plain>删除</el-button>--}}
                        </template>
                    </el-table-column>
                </el-table>
                <div class="pagination"  v-if="listData && listData.total">
                    <div class="pagination-total">共 <em> @{{listData.total}} </em> 条记录</div>
                    <el-pagination v-if="listData.last_page > 1"
                                   background
                                   :page-size="listData.per_page"
                                   layout="prev, pager, next, jumper"
                                   :total="listData.total"
                                   prev-text="上一页"
                                   next-text="下一页"
                                   :current-page="listData.current_page"
                                   @current-change="changePage">
                    </el-pagination>
                </div>
            </div>
        </div>
    </div>
@endsection
@section('scripts')
    <script src="{{ asset('js/moment.min.js') }}" type="text/javascript"></script>
    <script>
        let app = new Vue({
            el: '#app',
            data: function() {
                return {
                    listData: [],//列表数据
                    cateCascader: [],//连级类目
                    tabValue: 0,//tab
                    date: null,//日期范围
                    params: {!! $params !!},
                    // params: {kwd:'', index_recomment:'', page:1, status:0, recommend:0, start:'', end:'', cid:0},
                    loading: false,
                    multipleSelection:[],//多选项
                    categoys: {!! $categorys !!},
                }
            },
            created(){
                this.list();
            },
            methods: {
                //分页
                changePage(val) {
                    this.params.page = val;
                    this.list();
                },
                //列表
                list(){
                    let that = this;
                    this.loading = true;
                    setTimeout(()=>{
                        $.get("{{ route('admin.store.list') }}",this.params,function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.listData = res.data;
                                that.loading = false;
                            }
                        },'JSON');
                    },500);
                },
                //预览详情
                show(id){
                    window.open("{{ route('home.store.store') }}" + "?id=" + id);
                },
                //编辑
                edit(id){
                    window.location.href = "{{ route('admin.store.edit') }}" + '?id=' + id;
                },
                //统计详情
                detail(id){
                    window.location.href = "{{ route('admin.store.detail') }}" + '?id=' + id;
                },
                //时间筛选
                pickerChange() {
                    if (this.date) {
                        this.params.start = moment(this.date[0]).format('YYYY-MM-DD');
                        this.params.end = moment(this.date[1]).format('YYYY-MM-DD');
                    } else {
                        this.params.start = '';
                        this.params.end = '';
                    }
                },
                //类目筛选
                cascaderChange(){
                    if (this.cateCascader.length) {
                        this.params.cid = this.cateCascader[this.cateCascader.length-1];
                    } else {
                        this.params.cid = 0;
                    }
                },
                //筛选
                search(){
                    this.params.page = 1;
                    switch (this.tabValue) {
                        case '1':
                            this.params.status = 1;
                            this.params.index_recomment = 0;
                            this.params.recommend = 0;
                            break;
                        case '2':
                            this.params.status = 2;
                            this.params.index_recomment = 0;
                            this.params.recommend = 0;
                            break;
                        case '3':
                            this.params.status = 3;
                            this.params.index_recomment = 0;
                            this.params.recommend = 0;
                            break;
                        case '4':
                            this.params.status = 4;
                            this.params.index_recomment = 0;
                            this.params.recommend = 0;
                            break;
                        case '5':
                            this.params.index_recomment = 2;
                            this.params.status = 0;
                            this.params.recommend = 0;
                            break;
                        case '6':
                            this.params.recommend = 2;
                            this.params.index_recomment = 0;
                            this.params.status = 0;
                            break;
                        case '7':
                            this.params.status = 5;
                            this.params.recommend = 0;
                            this.params.index_recomment = 0;
                            break;
                        default:
                            this.params.status = 0;
                            this.params.index_recomment = 0;
                            this.params.recommend = 0;
                            break;
                    }
                    this.list();
                },
                //锁定
                lock(id){

                    this.$prompt('请输入不通过原因', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        inputErrorMessage: '请输入不通过原因'
                    }).then(({ value }) => {
                        let that = this;
                        $.post("{{ route('admin.store.lock') }}",{id: id,reason:value, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '取消输入'
                        });
                    });
                },
                //激活
                active(id){
                    let that = this;
                    $.post("{{ route('admin.store.active') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                        if(res.code){
                            that.$message.error(res.msg);
                        }else{
                            that.list();
                        }
                    },'JSON');
                },
                //关店
                close(id){

                    this.$prompt('请输入下架原因', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        inputErrorMessage: '请输入下架原因'
                    }).then(({ value }) => {
                        let that = this;
                        $.post("{{ route('admin.store.close') }}",{id: id,reason: value, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {
                        this.$message({
                            type: 'info',
                            message: '取消输入'
                        });
                    });
                },
                //导出
                exports(){
                    if(this.multipleSelection.length == 0){
                        var msg = '确定要导出所有的店铺吗？';
                    }else {
                        var msg = '确定要导出选择的店铺吗？';
                    }
                    this.$confirm(msg, '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        window.location.href = "{{ route('admin.store.exports') }}"+'?ids='+this.multipleSelection.join('-');
                    }).catch(() => {});
                },

                // 多选删除
                multiDelete(){
                    if(this.multipleSelection.length == 0){
                        this.$message.error('请至少选择一项你要删除的项');return false;
                    }
                    this.destroy(this.multipleSelection);
                },
                handleSelectionChange(val) {
                    let select = [];
                    if(val.length > 0){
                        val.filter((item)=>{
                            if(select.indexOf(item.id) === -1){
                                select.push(item.id)
                            }
                        });
                        this.multipleSelection = select;
                    }else{
                        this.multipleSelection = [];
                    }
                },
                //删除
                destroy(id){
                    let that = this;
                    this.$confirm('确定要删除选中的文章信息吗？', '提示', {
                        confirmButtonText: '确定',
                        cancelButtonText: '取消',
                        type: 'warning'
                    }).then(() => {
                        $.post("{{ route('admin.store.destroy') }}",{id: id, _token: "{{csrf_token()}}"},function (res) {
                            if(res.code){
                                that.$message.error(res.msg);
                            }else{
                                that.list();
                            }
                        },'JSON');
                    }).catch(() => {});
                },
            }
        });
    </script>
@endsection
